package com.taiyi.renaissancemall.ui

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View.VISIBLE
import android.widget.TextView

import androidx.fragment.app.Fragment

import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.Utils.snack
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.bean.VersionUpdateBean
import com.taiyi.renaissancemall.ui.classification.ClassFragment
import com.taiyi.renaissancemall.ui.endorsement.EndorsementFragment
import com.taiyi.renaissancemall.ui.main.first.FirstFragment
import com.taiyi.renaissancemall.ui.main.personshare.PersonShareActivity
import com.taiyi.renaissancemall.ui.main.shoppingcar.ShoppingCarActivity
import com.taiyi.renaissancemall.ui.mine.MineFragment
import com.taiyi.renaissancemall.ui.nearby.NearbyFragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.properties.Delegates

class MainActivity : BaseActivity<MainView, MainPresent>(), MainView {
    private var lastFragment: Fragment? = null
    private var mainFragment = FirstFragment()
    private var classFragment = ClassFragment()
    private var nearbyFragment = NearbyFragment()
    private var endorsementFragment = EndorsementFragment()
    private var mineFragment = MineFragment()
    lateinit var updateDialog: Dialog
    lateinit var contentTv: TextView
    lateinit var forceUpdateTv: TextView
    lateinit var laterTv: TextView
    lateinit var updateTv: TextView
    lateinit var myVersionUpdateBean: VersionUpdateBean


    var backTime by Delegates.observable(0L) { _, old, new ->
        if (new - old < 2000) {
            finish()
        } else {
            bottomNavigationView.snack(getString(R.string.press_again_to_exit_the_app))
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        var type = intent?.getIntExtra("type", 0)
        if (type != 0) {
            bottomNavigationView.selectedItemId = R.id.first
        }

    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.statusBarDarkFont(false, 0.0f).init()
    }

    private fun initView() {
        updateDialog = Dialog(this, R.style.MyDialog)
        var inflate = LayoutInflater.from(this).inflate(R.layout.version_update_dialog_layout, null)
        updateDialog.setContentView(inflate)
        updateDialog.setCanceledOnTouchOutside(false)
        updateDialog.setCancelable(false)
        contentTv = inflate.findViewById(R.id.contentTv)
        forceUpdateTv = inflate.findViewById(R.id.forceUpdateTv)
        laterTv = inflate.findViewById(R.id.laterTv)
        updateTv = inflate.findViewById(R.id.updateTv)
        forceUpdateTv.setOnClickListener {
            var downUrl = myVersionUpdateBean?.downUrl
            openBrowser(this, downUrl)
        }
        laterTv.setOnClickListener {
            updateDialog.dismiss()
        }
        updateTv.setOnClickListener {
            var downUrl = myVersionUpdateBean?.downUrl
            openBrowser(this, downUrl)
        }
        shoppingCarTv.setOnClickListener {
            startActivity(Intent(this, ShoppingCarActivity::class.java))
        }
        shareTv.setOnClickListener {
            startActivity(Intent(this, PersonShareActivity::class.java))
        }
        bottomNavigationView.run {
            itemIconTintList = null
            itemTextAppearanceActive = R.style.BottomNavigationTextSizeStyle
            itemTextAppearanceInactive = R.style.BottomNavigationTextSizeStyle
            setOnNavigationItemSelectedListener {
                when (it.itemId) {
                    R.id.first ->
                        switchFragment(mainFragment)
                    R.id.classification ->
                        switchFragment(classFragment)
                    R.id.nearby -> {
                        switchFragment(nearbyFragment)
                    }
                    R.id.endorsement -> {
                        switchFragment(endorsementFragment)
                    }
                    R.id.mine ->
                        switchFragment(mineFragment)
                }

                true
            }
        }
        bottomNavigationView.selectedItemId = R.id.first
    }


    private fun switchFragment(fragment: Fragment) {
        var beginTransaction = supportFragmentManager.beginTransaction()
        if (null != lastFragment) {
            beginTransaction.hide(lastFragment!!)
        }
        if (fragment.isAdded) {
            beginTransaction.show(fragment)
        } else {
            beginTransaction.add(R.id.container, fragment)
        }
        beginTransaction.addToBackStack(null)
        lastFragment = fragment
        beginTransaction.commit()
    }


    override fun onBackPressed() {
        backTime = System.currentTimeMillis()
    }

    override fun getLayoutId(): Int = R.layout.activity_main

    override fun initPresenter(): MainPresent = MainPresent()

    override fun initViews(savedInstanceState: Bundle?) {
        initView()
    }

    override fun initData() {

        presenter.checkVersionUpdate()
    }

    override fun initEvent() {

    }

    override fun getVersionFailure(msg: String?) {

    }

    override fun getVersionSuccess(versionUpdateBean: VersionUpdateBean?) {
        myVersionUpdateBean = versionUpdateBean!!
        var appVersionCode = presenter.getAppVersionCode()
        var newVersionCode = versionUpdateBean?.versionUp?.toLong()
        if (newVersionCode != null) {
            if (newVersionCode > appVersionCode) {
                contentTv.text = versionUpdateBean?.introduce
                var force = versionUpdateBean?.isForce//0不是强制更新  1强制更新
                if ("0" == force) {
                    laterTv.visibility = VISIBLE
                    updateTv.visibility = VISIBLE
                } else {

                    forceUpdateTv.visibility = VISIBLE
                }
                updateDialog.show()
            }
        }
    }

    fun openBrowser(context: Context, url: String) {
        var intent = Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        // 注意此处的判断intent.resolveActivity()可以返回显示该Intent的Activity对应的组件名
        // 官方解释 : Name of the component implementing an activity that can display the intent
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            var componentName = intent.resolveActivity(context.getPackageManager());
            // 打印Log   ComponentName到底是什么
            context.startActivity(Intent.createChooser(intent, "请选择浏览器"));
        } else {
            toast("未找到手机中的浏览器")
        }
    }

}
